﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using System.Net;
using com.lover.common;
using System.IO;
using com.lover.astd.common.model;
using com.lover.common.http;

namespace com.lover.astd.common.partner.impl
{
    public class ZhulangLogin : LoginImplBase
    {
        public ZhulangLogin() : base() { }
        public override LoginResult login(ref List<Cookie> cookies, string verify_code = null, string extra = null)
        {
            LoginResult result = new LoginResult();
            HttpResult resp;
            string token_url = "http://td.zhulang.com/";
            resp = TransferMgr.doGetPure(token_url, ref cookies);
            if (resp == null) { result.StatusCode = LoginStatusCode.FailInGetToken; return result; }
            string phpsessionid = "";
            foreach (Cookie cookie in resp.getCookies())
            {
                if (cookie == null) continue;
                if (cookie.Name == "PHPSESSID") { phpsessionid = cookie.Value; break; }
            }
            if (phpsessionid == null || phpsessionid == "") { result.StatusCode = LoginStatusCode.FailInGetToken; return result; }

            logging();
            string loginurl = "http://game.zhulang.com/login.php";
            string data = String.Format("PHPSESSID={0}&reurl=http%253A%252F%252Ftd.zhulang.com%252F&username={1}&password={2}&imageField.x=0&imageField.y=0", 
                phpsessionid, _username, _password);
            
            resp = TransferMgr.doPostPure(loginurl, data, ref cookies);
            if (resp == null) { result.StatusCode = LoginStatusCode.FailInLogin; return result; }

            string retstr = resp.getContent();

            if (retstr.IndexOf("window.alert") >= 0)
            {
                result.StatusCode = LoginStatusCode.FailInLogin;
                return result;
            }

            string redirecturl = String.Format("http://td.zhulang.com/checkuser.php?s_id={0}", _acc.ServerId);

            processRedirect(redirecturl, result, ref cookies);
            return result;
        }
    }
}
